package com.cskaoyan.market.service.impl;

import com.cskaoyan.market.db.domain.*;
import com.cskaoyan.market.db.mapper.*;
import com.cskaoyan.market.service.MarketAdminService;
import com.cskaoyan.market.util.MyBatisUtil;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.HashMap;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @ClassName MarketAdminServiceImpl
 * @Description: TODO
 * @Author 远志 zhangsong@cskaoyan.onaliyun.com
 * @Date 2023/5/5 11:08
 * @Version V1.0
 **/
public class MarketAdminServiceImpl implements MarketAdminService {

    private long goodsTotal;

    //    绝对不能将 SqlSession 实例的引用放在一个类的静态域，甚至一个类的实例变量也不行。 也绝不能将 SqlSession 实例的引用放在任何类型的托管作用域中，比如 Servlet 框架中的 HttpSession。
    //SqlSession sqlSession;
    @Override
    public List<MarketAdmin> login(String username, String password) {
        //mybatis应该如何使用呢？\
        //获取加载当前类的类加载器，需要使用类加载器来帮助我们获取文件的路径、流
        //从工厂中获取一个sql会话
        SqlSession sqlSession = MyBatisUtil.getSession();
        //执行原理：根据接口以及我们提供的mapper映射文件，程序运行时，动态地帮助我们创建了mapper接口的一个实现类,该实现类里面的每个方法其实就是封装的对应的sql语句
        MarketAdminMapper adminMapper = sqlSession.getMapper(MarketAdminMapper.class);
        //当我们执行当前对象的方法时，那么执行的便是对应的sql语句
        MarketAdminExample marketAdminExample = new MarketAdminExample();
        //select * from market_admin where username = and password =
        marketAdminExample.or().andUsernameEqualTo(username).andPasswordEqualTo(password);
        List<MarketAdmin> marketAdmins = adminMapper.selectByExample(marketAdminExample);
        sqlSession.commit();
        sqlSession.close();
        return marketAdmins;
    }

    @Override
    public HashMap<String, Long> getDashboard() {
        /**
         * goodsTotal
         * userTotal
         * productTotal
         * orderTotal
         */
        SqlSession session = MyBatisUtil.getSession();

        HashMap<String, Long> result = new HashMap();

        MarketGoodsMapper goodsMapper = session.getMapper(MarketGoodsMapper.class);
        MarketGoodsExample goodsExample = new MarketGoodsExample();
        goodsExample.or();
        long goodsTotal = goodsMapper.countByExample(goodsExample);
        result.put("goodsTotal", goodsTotal);

        MarketUserMapper userMapper = session.getMapper(MarketUserMapper.class);
        MarketUserExample userExample = new MarketUserExample();
        userExample.or();
        long userTotal = userMapper.countByExample(userExample);
        result.put("userTotal", userTotal);

        MarketGoodsProductMapper productMapper = session.getMapper(MarketGoodsProductMapper.class);
        MarketGoodsProductExample productExample = new MarketGoodsProductExample();
        productExample.or();
        long productTotal = productMapper.countByExample(productExample);
        result.put("productTotal", productTotal);

        MarketOrderMapper orderMapper = session.getMapper(MarketOrderMapper.class);
        MarketOrderExample orderExample = new MarketOrderExample();
        orderExample.or();
        long orderTotal = orderMapper.countByExample(orderExample);
        result.put("orderTotal", orderTotal);

        session.close();
        return result;
    }
    public List<MarketAdmin> list(String page, String limit, String sort, String order, String userName) {
        SqlSession sqlSession = MyBatisUtil.getSession();
        MarketAdminMapper adminMapper = sqlSession.getMapper(MarketAdminMapper.class);
        MarketAdminExample marketAdminExample = new MarketAdminExample();
        Integer integerLimit = Integer.valueOf(limit);
        Integer integerPage = Integer.valueOf(page);
        List<MarketAdmin> marketAdmins = null;
        if (StringUtils.isEmpty(userName)) {
            marketAdminExample.orderBy(sort + " " + order).or().andDeletedEqualTo(false);
        } else {
            marketAdminExample.orderBy(sort + " " + order).or().andUsernameLike("%" + userName + "%").andDeletedEqualTo(false);
        }
        PageHelper.startPage(integerPage, integerLimit);
        marketAdmins = adminMapper.selectByExampleSelective(marketAdminExample, MarketAdmin.Column.id, MarketAdmin.Column.username, MarketAdmin.Column.avatar, MarketAdmin.Column.roleIds);
        sqlSession.commit();
        sqlSession.close();
        return marketAdmins;
    }

    @Override
    public List<MarketAdmin> listForAdminInfromByUserName(MarketAdmin marketAdmin) {
        SqlSession sqlSession = MyBatisUtil.getSession();
        MarketAdminMapper adminMapper = sqlSession.getMapper(MarketAdminMapper.class);
        MarketAdminExample marketAdminExample = new MarketAdminExample();
        marketAdminExample.or().andUsernameEqualTo(marketAdmin.getUsername()).andDeletedEqualTo(false);
        ;
        List<MarketAdmin> marketAdmins = adminMapper.selectByExample(marketAdminExample);
        sqlSession.commit();
        sqlSession.close();
        return marketAdmins;
    }

    @Override
    public MarketAdmin update(MarketAdmin marketAdmin) {
        SqlSession sqlSession = MyBatisUtil.getSession();
        MarketAdminMapper adminMapper = sqlSession.getMapper(MarketAdminMapper.class);
        LocalDateTime updateTime = LocalDateTime.now();
        marketAdmin.setUpdateTime(updateTime);
        int i = adminMapper.updateByPrimaryKeySelective(marketAdmin);
        sqlSession.commit();
        sqlSession.close();
        if (i != 0) {
            return marketAdmin;
        }
        return null;
    }

    @Override
    public List<MarketAdmin> create(MarketAdmin marketAdmin) {
        SqlSession sqlSession = MyBatisUtil.getSession();
        MarketAdminMapper adminMapper = sqlSession.getMapper(MarketAdminMapper.class);
        LocalDateTime now = LocalDateTime.now();
        marketAdmin.setAddTime(now);
        marketAdmin.setUpdateTime(now);
        marketAdmin.setLastLoginIp("");
        MarketAdmin.Deleted notDeleted = MarketAdmin.Deleted.NOT_DELETED;
        marketAdmin.setDeleted(notDeleted.value());
        if (listForAdminInfromByUserName(marketAdmin).size() != 0) {
            return null;
        }
        int insert = adminMapper.insert(marketAdmin);
        MarketAdminExample marketAdminExample = new MarketAdminExample();
        marketAdminExample.createCriteria().andUsernameEqualTo(marketAdmin.getUsername());
        List<MarketAdmin> marketAdmins = adminMapper.selectByExample(marketAdminExample);
        sqlSession.commit();
        sqlSession.close();
        if (insert != 0 && marketAdmins.size() != 0) {
            return marketAdmins;
        }
        return null;
    }

    //逻辑删除，返回1时删除正确，否则删除错误
    @Override
    public int delete(MarketAdmin marketAdmin) {
        List<MarketAdmin> marketAdmins = listForAdminInfromByUserName(marketAdmin);
        if (marketAdmins.size() == 0) {
            return 0;
        }
        System.out.println(marketAdmin);
        marketAdmin.setDeleted(true);
        SqlSession sqlSession = MyBatisUtil.getSession();
        MarketAdminMapper adminMapper = sqlSession.getMapper(MarketAdminMapper.class);
        int i = adminMapper.updateByPrimaryKeySelective(marketAdmin);
        sqlSession.commit();
        sqlSession.close();
        return i;
    }
}
